﻿using BLL;
using MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Loginter.驾驶员管理
{
    public partial class AddDriverInfo : System.Web.UI.Page
    {
        DriverBLL driverBLL = new DriverBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                driveeinfo();
            }
        }
        #region 获取修改的驾驶员信息
        private void driveeinfo()
        {
            //获取要修改的车队编号
            int driverid = Convert.ToInt32(Request.QueryString["DriverID"]);
            if (driverid == 0)
            {
                Response.Redirect("Maintenance_drive.aspx");
            }
            else
            {
                DriverMODEL teammodel = driverBLL.Getdrive_byid(driverid);
                if (teammodel != null)
                {
                    txt_name.Text = teammodel.Name;
                    rbl_sex.SelectedValue = teammodel.Sex.ToString();
                    txt_birth.Text = teammodel.Brith.ToString();
                    txt_phone.Text = teammodel.Phone;
                    txt_idcard.Text = teammodel.IDCard;
                    txt_teamid.Text = teammodel.FK_TeamID.ToString();
                    rbl_state.SelectedValue = teammodel.State.ToString();
                    txt_remark.Text = teammodel.Remark;
                    txt_checkintime.Text = teammodel.CheckInTime.ToString();
                }
            }
        }
        #endregion

        #region 修改驾驶员信息
        /// <summary>
        /// 修改驾驶员信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_add_Click(object sender, EventArgs e)
        {
            //获取添加的驾驶员信息
            string name = txt_name.Text.Trim();
            int sex = Convert.ToInt32(rbl_sex.SelectedValue);
            string phone = txt_phone.Text.Trim();
            string idcard = txt_idcard.Text.Trim();
            string teamid = txt_teamid.Text.Trim();
            int state = Convert.ToInt32(rbl_state.SelectedValue);
            string remark = txt_remark.Text.Trim();
            string birth = txt_birth.Text.Trim();
            string checktime = txt_checkintime.Text.Trim();
            //判断车队id
            if (!Regex.IsMatch(teamid, @"^[0-9]*[1-9][0-9]*$"))
            {
                lbl_Msg.Text = "您输入的车队ID不正确，id为正整数！";
                return;
            }
            //判断电话号码格式
            if (!Regex.IsMatch(phone, @"^1[35789]\d{9}$"))
            {
                lbl_Msg.Text = "您输入的手机号码格式不正确！";
                return;
            }
            //判断身份证号格式 身份证号码
            // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X 
            if (!Regex.IsMatch(idcard, @"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$"))
            {
                lbl_Msg.Text = "您输入的身份证号码格式不正确！";
                return;
            }
            //判断时间格式 生日
            if (!Regex.IsMatch(birth, @"^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$"))
            {
                lbl_Msg.Text = "您输入的生日格式不正确！";
                return;
            }
            //判断时间格式 添加时间
            if (!Regex.IsMatch(checktime, @"^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$"))
            {
                lbl_Msg.Text = "您输入的添加时间格式不正确！";
                return;
            }
            int driverid = Convert.ToInt32(Request.QueryString["DriverID"]);
            DriverMODEL model = new DriverMODEL()
            {
                Name = name,
                Sex = sex,
                Brith = Convert.ToDateTime(birth),
                Phone = phone,
                IDCard = idcard,
                FK_TeamID = Convert.ToInt32(teamid),
                State = state,
                Remark = remark,
                CheckInTime = Convert.ToDateTime(checktime),
                DriverID = driverid
            };
            int result = driverBLL.getdriver_Update(model);
            if (result > 0)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('修改成功！');location.href='DriverInfoList.aspx'</script>");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('修改失败！')</script>");
            }
        }
        #endregion
    }
}